package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户服务
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    //根据用户名查询数据库获取用户信息和关联的角色信息，同时需要查询角色关联的权限信息
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//查询用户基本信息，不包含用户的角色
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        //根据用户ID查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                //根据角色ID查询关联的权限
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                role.setPermissions(permissions);//让角色关联权限
            }
            user.setRoles(roles);//让用户关联角色
        }
        return user;
    }


    //马志强代码开始
    public void setFk(Integer id, Integer[] roleIds) {
        if (roleIds != null && roleIds.length > 0){
            for (Integer roleId : roleIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("user_id", id);
                map.put("role_id", roleId);
                userDao.setFK(map);
            }
        }
    }
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByCondition(QueryPageBean queryPageBean) {
        //使用分页查询助手进行查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //进行字符串拼接
        String queryString = queryPageBean.getQueryString();
        if (StringUtil.isNotEmpty(queryString)) {
            queryString = "%" + queryString + "%";
        }
        Page<User> page = userDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Role> findAllRole() {
        List<Role> allRole = userDao.findAllRole();

        return allRole;
    }

    @Override
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);
        Integer id = user.getId();
        setFk(id,roleIds);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public Integer[] findRoleById(Integer id) {
        return userDao.findRoleById(id);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        //修改基本信息
        userDao.editUser(user);
        //删除外键关系
        Integer id = user.getId();
        userDao.deleteFK(id);
        //重新添加外键关系
        setFk(id,roleIds);
    }

    @Override
    public void deleteById(Integer id) {
        //先删除外键关系
        userDao.deleteFK(id);
        //删除用户信息
        userDao.deleteById(id);
    }
    //马志强代码over
}
